﻿using System;
using System.Data;
using System.Data.SqlClient;

public class ParentClassLogin 
{
    #region form needed
    SqlDataAdapter sda = new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
    #endregion

    protected bool CeckLoginProgram(string User, string Pass)
    {
        bool resalt = false;
        try
        {  
            // 
            cmd.Connection = new Sql().OpneSqlConcetion();
            cmd.CommandTimeout = 30;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "CheckLoginProgram";
            cmd.Parameters.Clear();
            cmd.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[]{
             new System.Data.SqlClient.SqlParameter("@User",System.Data.SqlDbType.NVarChar,50)
            ,new System.Data.SqlClient.SqlParameter("@Pass",System.Data.SqlDbType.NVarChar,50)
            });
            cmd.Parameters[0].Value = User;
            cmd.Parameters[1].Value = Pass;
            if (resalt = Convert.ToBoolean(cmd.ExecuteScalar()))
                return resalt = true;
        }
        catch
        {
            throw new ArgumentException("Error 301");
        }
        return resalt;

    }
    
    protected int p_insertUserprogram(string user, string pass, string nameandfamily, string nationalcode, string address, string mob, string tel)
    {
        
        try
        {
            cmd.Connection = new Sql().OpneSqlConcetion();
            cmd.CommandTimeout = 30;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "spInsertUserProgram";
            cmd.Parameters.Clear();
            cmd.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[]{
            new System.Data.SqlClient.SqlParameter("@id",SqlDbType.Int),
            new System.Data.SqlClient.SqlParameter("@user",SqlDbType.NVarChar,50),
            new System.Data.SqlClient.SqlParameter("@pass",SqlDbType.NVarChar,50),
            new System.Data.SqlClient.SqlParameter("@nandf",SqlDbType.NVarChar,50),
            new System.Data.SqlClient.SqlParameter("@nc",SqlDbType.Char,10),
            new System.Data.SqlClient.SqlParameter("@address",SqlDbType.NVarChar,250),
            new System.Data.SqlClient.SqlParameter("@mobile",SqlDbType.Char,11),
            new System.Data.SqlClient.SqlParameter("@tel",SqlDbType.VarChar,20),
            });
            int tt = ReturnIdFromUserProgram();
            cmd.Parameters[0].Value = tt++;
            cmd.Parameters[1].Value = user;
            cmd.Parameters[2].Value = pass;
            cmd.Parameters[3].Value = nameandfamily;
            cmd.Parameters[4].Value = nationalcode;
            cmd.Parameters[5].Value = address;
            cmd.Parameters[6].Value = mob;
            cmd.Parameters[7].Value = tel;
            if (Convert.ToInt32(cmd.ExecuteNonQuery()) == 1)
            {
                cmd.Connection.Close();
                return 1;
            }
        }
        catch
        { throw new ArgumentException("Error 302"); }
        return 0;
    }
    private int ReturnIdFromUserProgram()
    {
        int iduser = 0;
        try
        {
            cmd.CommandText = "SP_ReturnIDUserProgram";
            sda.SelectCommand = cmd;
            ds.Tables.Clear();
            sda.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
                return iduser++;
            else
                return iduser = ds.Tables[0].Rows.Count;
        }
        catch
        {
            throw new ArgumentException("Error 303");
        }

        return iduser;
    }
}

